package com.opgea.tutor.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.opgea.tutor.dao.AbstractDAO;
import com.opgea.tutor.dao.TutorDocumentDAO;
import com.opgea.tutor.domain.entities.TutorDocument;

@Repository
public class TutorDocumentDAOImpl extends AbstractDAO<TutorDocument> implements
	TutorDocumentDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public TutorDocument find(Long documentId) {
	@SuppressWarnings("unchecked")
	List<TutorDocument> objects = sessionFactory.getCurrentSession()
		.createCriteria(TutorDocument.class)
		.add(Restrictions.eq("id", documentId)).list();
	System.out.println(objects.get(0));
	return objects.get(0);
    }

    @Override
    public TutorDocument findDocumentByType(Long tutorId, String documentType) {
	//String queryString = "from TutorDocument d WHERE d.tutor.id =:id AND d.documentType =:documentType";
	String queryString = "from TutorDocument d";
	Query query = sessionFactory.getCurrentSession().createQuery(
		queryString);
	//query.setParameter("id", tutorId);
	//query.setParameter("documentType", documentType);

	System.out.println("QueryString: " + query.getQueryString());
	TutorDocument document = (TutorDocument) query.list().get(0);
	return document;
    }
}
